<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible" width="800px">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
          <el-row>
       <el-col :span="12">
    <el-form-item label="类型" prop="type">
      <el-input v-model="dataForm.type" placeholder="如：wdsource.net、163.com"></el-input>
    </el-form-item>
    </el-col>
      </el-row>
      <el-row>
       <el-col :span="12">
    <el-form-item label="服务器" prop="service">
      <el-input v-model="dataForm.service" placeholder="如：SMTP服务器：smtp.wdsource.net"></el-input>
    </el-form-item>
    </el-col>
     <el-col :span="12">
    <el-form-item label="端口" prop="port">
      <el-input v-model="dataForm.port" placeholder="端口"></el-input>
    </el-form-item>
    </el-col>
      </el-row>
      <el-row>
       <el-col :span="12">
    <el-form-item label="邮箱地址" prop="addressee">
      <el-input v-model="dataForm.addressee" placeholder="邮箱地址"></el-input>
    </el-form-item>
    </el-col>
    <el-col :span="12">
    <el-form-item label="邮箱密码" prop="password">
      <el-input v-model="dataForm.password" placeholder="邮箱密码"></el-input>
    </el-form-item>
    </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          type: '',
          service: '',
          port: '',
          addressee: '',
          password: '',
          status: 1,
          addUser: '',
          addTime: '',
          editUser: '',
          editTime: ''
        },
        dataRule: {
          type: [
            { required: true, message: '类型不能为空', trigger: 'blur' }
          ],
          service: [
            { required: true, message: '服务器不能为空', trigger: 'blur' }
          ],
          port: [
            { required: true, message: '端口不能为空', trigger: 'blur' }
          ],
          addressee: [
            { required: true,validator: this.checkEmail, trigger: 'blur' }
          ],
          password: [
            { required: true, message: '邮箱密码不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          this.dataForm.status=1
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/emsgsetup/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.type = data.emsgSetup.type
                this.dataForm.service = data.emsgSetup.service
                this.dataForm.port = data.emsgSetup.port
                this.dataForm.addressee = data.emsgSetup.addressee
                this.dataForm.password = data.emsgSetup.password
                this.dataForm.status = data.emsgSetup.status
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/emsgsetup/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'type': this.dataForm.type,
                'service': this.dataForm.service,
                'port': this.dataForm.port,
                'addressee': this.dataForm.addressee,
                'password': this.dataForm.password,
                'status': this.dataForm.status,
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
         /**
     * 邮箱校验
     */
    checkEmail(rule, value, callback) {
    const mailReg = (/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/)
    if (!value) {
      return callback(new Error("邮箱不能为空"))
    }
    setTimeout(() => {
      if (mailReg.test(value)) {
        callback()
      } else {
        callback(new Error("请输入正确的邮箱格式"))
      }
    }, 100)
  },
    }
  }
</script>
